package com.tb.dubbo_service;

import com.tb.dubbo.IGoodsAddService;
import com.tb.mapper.GoodsMapper;
import com.tb.mapper.ImgMapper;
import com.tb.mapper.NatureMapper;
import com.tb.mapper.NatureValueMapper;
import com.tb.pojo.*;
import com.tb.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class GoodsAddSeviceImpl implements IGoodsAddService {

   @Autowired
   private GoodsMapper goodsMapper;

   @Autowired
   private NatureMapper natureMapper;

   @Autowired
   private NatureValueMapper natureValueMapper;

   @Autowired
   private ImgMapper imgMapper;

    @Override
    public boolean addGoods(Goods2 goods2) {
        /*
        第一步，拿到商品表相关的数据
         */
        boolean b=false;
        Goods goods=new Goods();
        //生成商品ID
        long goodsId=IdUtils.genItemId();
        goods.setGoodsId(String.valueOf(goodsId));

        goods.setGoodsName(goods2.getGoodsName());
        goods.setGoodsPrice(goods2.getGoodsPrice());
        goods.setGoodsNum(goods2.getGoodsNum());
        goods.setGoodsWeight(goods2.getGoodsWeight());
        goods.setGoodsKind(goods2.getGoodsKind());
        goods.setGoodsDiscount(goods2.getGoodsDiscount());
        //添加商品
        int gs=goodsMapper.insertSelective(goods);
        //商品图片
        List<Img> img = goods2.getImg();
        int inm=0;
        for (Img is : img) {
            Img im=new Img();
            im.setGoodsId(String.valueOf(goodsId));
            im.setImgUrl(is.getImgUrl());
            //添加图片
            inm=imgMapper.insertSelective(im);
        }

        /*
        第二步，拿到nature表相关的数据
         */
        List<Nature> natures = goods2.getNatures();
        int i=0;
        for (Nature nature1 : natures) {
            Nature nature=new Nature();
            Random random=new Random();

            i = random.nextInt(100000);
            nature.setId(i);
            nature.setGoodsId(String.valueOf(goodsId));
            nature.setNature(nature1.getNature());
            //添加nature
            int in = natureMapper.insertSelective(nature);
             /*
        第三步，拿到nature_value相关的数据
         */
            List<NatureValue> naturevs = nature1.getNaturev();
            for (NatureValue naturev : naturevs) {
                NatureValue natureValue=new NatureValue();
                natureValue.setNatureId(i);
                Map<String, String> map = naturev.getMap();
                for (Map.Entry<String,String> entry:map.entrySet()){
                   // System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                    String key=entry.getKey();
                    String value=entry.getValue();
                    natureValue.setNatureKey(key);
                    natureValue.setNatureValue(value);
                }
                //添加详情
                int inv = natureValueMapper.insertSelective(natureValue);

                if (gs>=1&&in>=1&&inv>=1&&inm>=1){
                    b=true;
                }
                //  natureValue.setMap(naturev.getMap());
            }
        }
        return b;
    }
}
